<?php
/**
 * 获取导入的csv中的数据
 * @param string $file_name
 * @param 是否需要首行数据 $needFirstRow
 * @return multitype:multitype: |string
 */
function getCsvData($file_name, $needFirstRow = 0)
{
	if ($_FILES[$file_name]) {
// 		$data = file_get_contents();
// 		$data = iconv('gb2312', 'UTF-8', $data);
		
		$handle = fopen($_FILES[$file_name]['tmp_name'], "r");
		$rows = array();
		$i = 0;
		while (($data = fgetcsv($handle, 4096)) !== FALSE) {
			if ($i == 0 && $needFirstRow == 0) {
				$i || $i = 1;
				continue;
			}
			$data = array_map('converToUtf8', $data);
			$rows[] = $data;
			
		}
		fclose($handle);
		unlink($_FILES[$file_name]['tmp_name']);
		return $rows;
	} else {
		return '';
	}
}

/**
 * 编码转换
 * @param unknown_type $data
 * @return string
 */
function converToUtf8($data)
{
	return iconv('gb2312', 'UTF-8', $data);
}

/**
 * 数组转成csv字符串
 * @param array $fields
 * @param unknown_type $delimiter
 * @param unknown_type $enclosure
 * @param unknown_type $encloseAll
 * @param unknown_type $nullToMysqlNull
 * @return string
 */
function array_to_csv(array &$fields, $delimiter = ';', $enclosure = '"', $encloseAll = false, $nullToMysqlNull = false)
{
	$delimiter_esc = preg_quote($delimiter, '/');
	$enclosure_esc = preg_quote($enclosure, '/');

	$output = array();
	foreach ( $fields as $field ) {
		if ($field === null && $nullToMysqlNull) {
			$output[] = 'NULL';
			continue;
		}

		// Enclose fields containing $delimiter, $enclosure or whitespace
		if ( $encloseAll || preg_match( "/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field ) ) {
			$output[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure;
		}
		else {
			$output[] = $field;
		}
	}
	
	return implode( $delimiter, $output );
}

function array_to_table($data, $is_output = 0)
{
	if ($is_output) {
		echo '<table border="1" cellpadding="1" cellspacing="1">';
	} else {
		$html = '<table border="1" cellpadding="1" cellspacing="1">';
	}
	
	foreach ($data as $tr) {
		
		if ($is_output) {
			echo '<tr>';
		} else {
			$html .= '<tr>';
		}
		
		foreach ($tr as $td) {
			if ($is_output) {
				echo "<td>{$td}</td>";
			} else {
				$html .= "<td>{$td}</td>";
			}
		}
		
		if ($is_output) {
			echo '</tr>';
		} else {
			$html .= '</tr>';
		}
	}
	
	if ($is_output) {
		echo '</table>';
	} else {
		$html .= '</table>';
		return $html;
	}
}

/**
 * 导出csv
 * @param unknown_type $filename
 * @param unknown_type $data
 */
function export_csv($filename, $data) {
	header("Content-type:text/csv");
	header("Content-Disposition:attachment;filename=".$filename);
	header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
	header('Expires:0');
	header('Pragma:public');
	if (is_array($data)) {
		array_to_csv($data);
	} else {
		echo $data;
	}
}

/**
 * 导出csv
 * @param unknown_type $filename
 * @param unknown_type $data
 */
function export_html($filename, $data) {
	header("Content-type:text/html");
	header("Content-Disposition:attachment;filename=".$filename);
	header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
	header('Expires:0');
	header('Pragma:public');
	if (is_array($data)) {
		array_to_table($data, 1);
	} else {
		echo $data;
	}
}